package vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.actions;

import vg.services.data_base_manager.data.graph.Graph;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.graph_view.IGraphRenderer;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.SmartGraphView;
import vg.services.main_manager.MainManager;

import javax.swing.*;

/**
 * Make merge of two graph representations.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class MergeAction extends DefaultActionAdapter {
	// Main data
	private AGraphView srcGraphView;
	private Graph srcGraph;
	private boolean completed = false;
	
	// Mutex
	private final Object generalMutex = new Object();
	
	public MergeAction(AGraphView srcGraphView, Graph srcGraph) {
		super("Merge patch");
		
		this.srcGraphView = srcGraphView;
		
		this.srcGraph = srcGraph;
	}
	
	@Override
	public void noEDTAction(final IGraphRenderer dstGraphRenderer) {
		// lock src and dst
        srcGraphView.addAction(new DefaultActionAdapter("Merge patch") {
            @Override
            public void action(final IGraphRenderer srcGraphRenderer) {
                synchronized (generalMutex) {
                    try {
                        dstGraphRenderer.merge(srcGraph, srcGraphRenderer);
                    } finally {
                        completed = true;
                    }
                }
            }
        });

        // wait
		while(true) {
			try {
				Thread.sleep(100);
			} catch (Exception ex) {
                MainManager.logger.printException(ex);
            }
			
			synchronized (generalMutex) {
				if (completed)
					break;
			}
		}
	}
}
